package cn.chinaunicom.sdsi.upfc_demp.uploadtest.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.upfc_demp.sys.dao.mybatis.SysDemoMapper;
import cn.chinaunicom.sdsi.upfc_demp.sys.entity.SysDemo;
import cn.chinaunicom.sdsi.upfc_demp.sys.service.SysDemoService;
import cn.chinaunicom.sdsi.upfc_demp.uploadtest.service.FileUploadService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class FileUploadServiceImpl extends BaseService implements FileUploadService{
	@Autowired
	private SysDemoMapper sysDemoMapper;
	
	@Override
	public PageInfo<SysDemo> searchDemo4Page(SysDemo sd, Integer start,
			Integer pageSize) {
		
		setPageInfo(start, pageSize);
		
		List<SysDemo>sdList = sysDemoMapper.search4Page(sd);
		PageInfo<SysDemo> page = new PageInfo<SysDemo>(sdList);
		
		return page;
	}
	
	@Override
	public JSONObject searchData4Page(SysDemo sds, Integer start, Integer pageSize) {
        // setPageInfo方法 和 mapper的查询方法必须相邻，中间不能有其他查询语句，否则mybatis的分页机制会不生效
		setPageInfo(start,pageSize);
		List<SysDemo>sdsList = sysDemoMapper.search4Page(sds);
		PageInfo<SysDemo> page = new PageInfo<SysDemo>(sdsList);
		JSONObject obj = null;
		JSONArray array = new JSONArray();
		for(SysDemo sd :sdsList){
			obj = new JSONObject();
			obj.put("id",sd.getId());
			obj.put("name", sd.getName());
			obj.put("author",sd.getAuthor());
			obj.put("status",sd.getStatus());
			array.add(obj);
		}
		obj = new JSONObject();
		obj.element("data", array);
		obj.element("recordsTotal", page.getTotal());
		obj.element("recordsFiltered", page.getTotal());
		obj.element("draw", 0);
		return obj;
	}

}
